package com.carrental.vehicle.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.carrental.common.result.Result;
import com.carrental.vehicle.entity.Image;
import com.carrental.vehicle.service.ImageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 图片控制器
 */
@Slf4j
@Api(tags = "图片管理")
@RestController
@RequestMapping("/image")
@RequiredArgsConstructor
public class ImageController {

    private final ImageService imageService;

    /**
     * 分页查询图片
     */
    @ApiOperation("分页查询图片")
    @GetMapping("/page")
    public Result<IPage<Image>> pageList(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页数量") @RequestParam(defaultValue = "24") Integer size,
            @ApiParam("分类ID") @RequestParam Long categoryId,
            @ApiParam("图片名称") @RequestParam(required = false) String name) {
        IPage<Image> page = imageService.pageList(current, size, categoryId, name);
        return Result.success(page);
    }

    /**
     * 获取图片详情
     */
    @ApiOperation("获取图片详情")
    @GetMapping("/{id}")
    public Result<Image> getById(@ApiParam("图片ID") @PathVariable Long id) {
        Image image = imageService.getById(id);
        return Result.success(image);
    }

    /**
     * 新增图片
     */
    @ApiOperation("新增图片")
    @PostMapping
    public Result<Boolean> add(@ApiParam("图片信息") @RequestBody Image image) {
        boolean result = imageService.addImage(image);
        return Result.success(result);
    }

    /**
     * 修改图片
     */
    @ApiOperation("修改图片")
    @PutMapping
    public Result<Boolean> update(@ApiParam("图片信息") @RequestBody Image image) {
        boolean result = imageService.updateImage(image);
        return Result.success(result);
    }

    /**
     * 删除图片
     */
    @ApiOperation("删除图片")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@ApiParam("图片ID") @PathVariable Long id) {
        boolean result = imageService.deleteImage(id);
        return Result.success(result);
    }

    /**
     * 批量删除图片
     */
    @ApiOperation("批量删除图片")
    @DeleteMapping("/batch")
    public Result<Boolean> batchDelete(@ApiParam("图片ID列表") @RequestBody List<Long> ids) {
        boolean result = imageService.batchDeleteImage(ids);
        return Result.success(result);
    }

    /**
     * 根据分类ID获取图片列表
     */
    @ApiOperation("根据分类ID获取图片列表")
    @GetMapping("/list/{categoryId}")
    public Result<List<Image>> getImagesByCategoryId(@ApiParam("分类ID") @PathVariable Long categoryId) {
        List<Image> images = imageService.getImagesByCategoryId(categoryId);
        return Result.success(images);
    }
} 